var jc = {
    convert_to_string: function (json, err) {
        json.splice(0, 1)
        
        //转置
        var out_put = [];
        var key_row = json[0];
        for (var j = 1; j < json.length; j++) {
            var data = json[j];
            var json_item = {};
            for (var i = 0; i < key_row.length; i++) {
                var str = key_row[i];
                if(!str || str.indexOf(":")<0)
                {
                    // err.msg = "字段格式错误,无冒号:"+str;
                    // return "";
                    continue;
                }
                var keymix = str.split(":");
                var key = keymix[0];
                var type = keymix[1];
                switch (type) {
                    case 'n':
                        {
                            if(typeof data[i] == "undefined")
                                json_item[key] = 0;
                            else{
                                var value = parseInt(data[i]);
                                if(isNaN(value)){
                                    value = 0
                                } else {
                                         var regex = new RegExp("^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$"); 
                                         if (!regex.test(data[i]) || isNaN(value))
                                         {    
                                            err.msg = "key:" + key + " 无效的内容:" + data[i];
                                            return "";
                                         }  
                                }

                                json_item[key] = value;
                            }
                        }
                        break;
                    case 'f':
                        {
                            if(typeof data[i] == "undefined")
                                json_item[key] = 0;
                            else{
                                var value = parseFloat(data[i]);
                                 var regex = new RegExp("^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$"); 
                                 if (!regex.test(data[i]) || isNaN(value))
                                 {    
                                    err.msg = "key:" + key + " 无效的内容:" + data[i];
                                    return "";
                                 }  

                                json_item[key] = value;
                            }
                        }
                        break;
                    case 'b':
                        if(typeof data[i] == "undefined")
                            json_item[key] = false;
                        else
                        {
                            switch (data[i]) {
                                case "0":
                                    json_item[key] = false;
                                    break;
                                default:
                                    json_item[key] = true;
                                    break;
                            }
                        }
                        break;
                    case 's':
                        json_item[key] = data[i];
                        if(json_item[key] == null)
                        json_item[key] = ""
                        break;
                    case "j":
                        if (data[i] == "null") {
                            json_item[key] = null;
                        }
                        try{
                            json_item[key] = JSON.parse(data[i]);
                        } catch (e) {
                            err.msg = "json类型数据错误:"+e;
                        }
                        break;
                    default:
                       // alert(type);
                        err.msg = "无效的类型:"+str
                        return "{}";
                }
            }
            out_put.push(json_item);
        }
        return JSON.stringify(out_put);
    }
}

export default jc;